package com.yellow.car.impl;

import com.yellow.car.api.PathApi;
import com.yellow.car.api.StationApi;
import com.yellow.car.api.YellowCarApi;
import com.yellow.car.entity.CyclingYellowCar;
import com.yellow.car.entity.YellowCar;
import com.yellow.car.request.PathQueryRequest;
import com.yellow.car.request.YellowCarCyclingRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class YellowCarApiImpl implements YellowCarApi {
    private static YellowCar yellowCar;
    private static List<CyclingYellowCar> cyclingEndTimeCar = new ArrayList<>();
    @Resource
    PathApi pathApi;
    @Resource
    StationApi stationApi;

    @Override
    public void init() {
        //初始化小黄车
        yellowCar = new YellowCar(1);
    }

    @Override
    public void cycling(YellowCarCyclingRequest request) {
        PathQueryRequest pathQueryRequest = new PathQueryRequest();
        pathQueryRequest.setStartStation(request.getStartStationName());
        pathQueryRequest.setEndStation(request.getEndStationName());
        int[][] path = pathApi.getPath(pathQueryRequest);
        Boolean cyclingFlag = stationApi.reduceByStationName(request.getStartStationName(), 1);
        if (cyclingFlag) {
            int cyclingTime = path.length / yellowCar.getSpeed();
            long currentTimeMillis = System.currentTimeMillis();
            long endTimeMillis = currentTimeMillis + cyclingTime * 1000L;
            CyclingYellowCar car = new CyclingYellowCar();
            car.setStartStationName(request.getStartStationName());
            car.setEndStationName(request.getEndStationName());
            car.setCyclingEndTime(endTimeMillis);
            cyclingEndTimeCar.add(car);
        }
    }

    @Override
    public List<CyclingYellowCar> getCyclingCar() {
        return cyclingEndTimeCar;
    }

    @Override
    public void removeCyclingCar(CyclingYellowCar car) {
        cyclingEndTimeCar.remove(car);
    }
}
